/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.view.page;

import at.irian.demo.jsfatwork.domain.Vote;
import at.irian.demo.jsfatwork.domain.Answer;
import at.irian.demo.jsfatwork.service.VotingService;
import at.irian.demo.jsfatwork.view.security.UserHolder;
import at.irian.demo.jsfatwork.view.PageBean;

import javax.inject.Named;
import javax.inject.Inject;
import java.util.List;
import java.util.ArrayList;

/**
 * gets a separated entity manager (different conversation) -> repository has to merge
 *
 * @author Gerhard Petracek
 */
@PageBean
@Named("voteDetailPage")
public class VoteDetailPage extends AbstractPage
{
    @Inject
    private UserHolder userHolder;

    @Inject
    private VotingService votingService;

    private Vote currentVote;

    private Answer selectedAnswer;

    private List<Answer> remainingAnswers;

    void setCurrentVote(Vote currentVote)
    {
        this.currentVote = currentVote;
        this.remainingAnswers = buildRemainingAnswers(currentVote);
    }

    private List<Answer> buildRemainingAnswers(Vote currentVote)
    {
        List<Answer> result = new ArrayList<Answer>(currentVote.getPoll().getAnswers());

        for(Answer votedAnswer : currentVote.getAnswers())
        {
            result.remove(votedAnswer);
        }

        return result;
    }

    public void addAnswer()
    {
        this.currentVote.addAnswer(this.selectedAnswer);
        this.remainingAnswers.remove(this.selectedAnswer);
    }

    public void removeAnswer()
    {
        this.currentVote.getAnswers().remove(this.selectedAnswer);
        this.remainingAnswers.add(this.selectedAnswer);
    }

    public String saveChanges()
    {
        this.votingService.save(this.currentVote);
        cleanup();

        return "pages/polls/findPoll";
    }

    /*
    * generated
    */

    public Vote getCurrentVote()
    {
        return currentVote;
    }

    public void setSelectedAnswer(Answer selectedAnswer)
    {
        this.selectedAnswer = selectedAnswer;
    }

    public List<Answer> getRemainingAnswers()
    {
        return remainingAnswers;
    }
}